package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.EquipmentBindingWorkAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.BinWorkBean;
import com.glory.brepda.entity.DefectCodeEntity;
import com.glory.brepda.entity.MultimastereqpbindingBean;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class EquipmentBindingWorkActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.etWorkID)
    FreeUI_ClearEditText etWorkID;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.rvBingList)
    RecyclerView rvBingList;
    @BindView(R.id.tvReset)
    TextView tvReset;
    @BindView(R.id.tvClear)
    TextView tvClear;
    private EquipmentBindingWorkAdapter mEquipmentBindingWorkAdapter;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_equipmentbindingwork;
    }

    @Override
    protected void initView() {
        tvBaseSubmit.setText("解绑设备");
        setMainLayout(llMain);
        tvReset.setVisibility(View.VISIBLE);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        initRecyclerView(rvBingList, this);
        mEquipmentBindingWorkAdapter = new EquipmentBindingWorkAdapter(null);
        mEquipmentBindingWorkAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mEquipmentBindingWorkAdapter.isFirstOnly(true);
        rvBingList.setAdapter(mEquipmentBindingWorkAdapter);

    }

    @Override
    protected void initData() {

        initEdit(etEqpID, s->{
            FreeUI_EntityCallBack<BaseResponseBean<MultimastereqpbindingBean>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<MultimastereqpbindingBean>>
                    (new TypeToken<BaseResponseBean<MultimastereqpbindingBean>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<MultimastereqpbindingBean>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        response.body().getResponse().getBody().getEquipmentlist().contains(mEquipmentBindingWorkAdapter.getData());
                        mEquipmentBindingWorkAdapter.addData(response.body().getResponse().getBody().getEquipmentlist());
                        etEqpID.setText("");
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<MultimastereqpbindingBean>> response) {
                    super.onError(response);
                    loadError(response.getException(), "GETSTEPBYLOT");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "MultiMasterEqpQuery");
            map.addProperty("WOID", getText(etWorkID));
            map.addProperty("EQUIPMENTID", s);
            NetUtils.requestPostNet("IF.MULTIMASTEREQPBINDING", "Common", EquipmentBindingWorkActivity.this, map, callback);

        });

        initEdit(etWorkID,s ->{
            FreeUI_EntityCallBack<BaseResponseBean<MultimastereqpbindingBean>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<MultimastereqpbindingBean>>
                    (new TypeToken<BaseResponseBean<MultimastereqpbindingBean>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<MultimastereqpbindingBean>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        mEquipmentBindingWorkAdapter.getData().clear();
                        response.body().getResponse().getBody().getEquipmentlist().contains(mEquipmentBindingWorkAdapter.getData());
                        mEquipmentBindingWorkAdapter.addData(response.body().getResponse().getBody().getEquipmentlist());
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<MultimastereqpbindingBean>> response) {
                    super.onError(response);
                    loadError(response.getException(), "GETSTEPBYLOT");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "MultiMasterEqpQuery");
            map.addProperty("WOID", s);
            map.addProperty("EQUIPMENTID", getText(etEqpID));
            NetUtils.requestPostNet("IF.MULTIMASTEREQPBINDING", "Common", EquipmentBindingWorkActivity.this, map, callback);

        });

    }
    @OnClick({R.id.tvBaseSubmit, R.id.tvClear, R.id.tvReset})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                BinWork("MultiMasterEqpEmpty");
                break;
            case R.id.tvClear:
                BinWork("MultiMasterEqpUnbingOrder");
                break;
            case R.id.tvReset:
                BinWork("MultiMasterEqpReset");
                break;
        }
    }

    private void BinWork(String actiontype) {
        FreeUI_EntityCallBack<BaseResponseBean<MultimastereqpbindingBean>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<MultimastereqpbindingBean>>
                (new TypeToken<BaseResponseBean<MultimastereqpbindingBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<MultimastereqpbindingBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    if ("MultiMasterEqpEmpty".equals(actiontype)){
                        MultimastereqpbindingBean multimastereqpbindingBean = new MultimastereqpbindingBean();
                        mEquipmentBindingWorkAdapter.getData().clear();
                        response.body().getResponse().getBody().getEquipmentlist().contains(mEquipmentBindingWorkAdapter.getData());
                        mEquipmentBindingWorkAdapter.addData(multimastereqpbindingBean.getEquipmentlist());
                    }else {
                        mEquipmentBindingWorkAdapter.getData().clear();
                        response.body().getResponse().getBody().getEquipmentlist().contains(mEquipmentBindingWorkAdapter.getData());
                        mEquipmentBindingWorkAdapter.addData(response.body().getResponse().getBody().getEquipmentlist());
                    }
                    showToast("提交成功",true);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<MultimastereqpbindingBean>> response) {
                super.onError(response);
                loadError(response.getException(), "GETSTEPBYLOT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", actiontype);
        map.addProperty("WOID", getText(etWorkID));
        map.add("EQUIPMENTLIST", new JsonParser().parse(new Gson().toJson(mEquipmentBindingWorkAdapter.getData())).getAsJsonArray());//传批次集合
        NetUtils.requestPostNet("IF.MULTIMASTEREQPBINDING", "Common", EquipmentBindingWorkActivity.this, map, callback);

    }

}
